import jieba

# 读取文本
with open('华为手机评论.txt', 'r', encoding='utf-8') as file:
    s = file.read()
# print(s)

# 分词
lst = jieba.lcut(s)
# print(lst)

# 去重
set1 = set(lst)  # 使用集合去重

# 统计出现次数
d = {}  # 定义一个字典。key:词  value:出现的次数
for item in set1:
    if len(item) >= 2:
        d[item] = 0
# print(d)

for item in lst:
    if item in d:
        d[item] = d.get(item) + 1
# print(d)

# 字典转成列表
new_lst = []
for item in d:
    new_lst.append([item, d[item]])
# print(new_lst)

# 转成列表为了排序
new_lst.sort(key=lambda x: x[1], reverse=True)
print(new_lst[0:11])  # 显示前十项

input()
